import ErrorCode from "../script/ErrorCode";
import { Toast } from "vant";

export const changePassword = {
  
  created() {},
  methods: {
    initialize() {
      this.send.phoneNumber = "";   // 加密手机号
      this.send.password = "";// 加密后原密码
      this.send.newPassword = "";// 加密手机号
      this.send.loginVersion = "";// 登陆版本
      this.send.clientVersion = "";// 客户端版本
      this.send.clientDeviceId = "";// 设备id
    },
    //提示语
    hint(status, txt) {
      this[status] = txt;
      const self = this;
      setTimeout(() => {
        self[status] = "";
      }, 1000);
    },
    submit() {
      const self = this;
      if (!this.$validate.notEmpty.regex.test(this.send.phoneNumber)) {
        return this.hint("errors", this.$validate.phone.error);
      }
       if (!this.$validate.notEmpty.regex.test(this.send.password)) {
        return this.hint("errors", this.$validate.password.error);
      }
      if (!this.$validate.newpassword.regex.test(this.send.newPassword)) {
        return this.hint("errors", this.$validate.newpassword.error);
      }
      if (!navigator.onLine) return Toast("网络连接失败，请检查网络设置");
       
      const { version,clientVersion, clientDeviceId} = this.$fn.getUrlParams();
      this.$store.dispatch("updatePassword", {
          phoneNumber: this.$fn.encryptByDES(this.send.phoneNumber),
          password: this.$fn.encryptByDES(this.send.password),
          newPassword: this.$fn.encryptByDES(this.send.newPassword),
          // loginVersion:this.send.loginVersion,
          loginVersion: !version || version == 1 ? 2 : version,
          clientVersion:clientVersion!='undefined'?clientVersion:'',
          clientDeviceId:clientDeviceId!='undefined'?clientDeviceId:'',
        }) .then(function(data) {  
          if (data.status == 1) {
            self.rockon();//修改成功
             return Toast("修改成功");
          } else {
            self.showError(ErrorCode[data.status]);
          }
        });
    },
    showError(val) {
      this.hint("errors", val);
    },
    rockon() {
      const self = this,
        timer = setInterval(() => {
          // if (self.reciprocal == 1) {
            clearInterval(timer);
            return self.toggleType();
          // }
          // self.reciprocal--;
        }, 1000);
    },
    toggleType(num) {
      //调用登录窗口
      window.prompt(
        "",
        `up://["web_user_login",${Date.now()},{"type":"client_login","data":{}}]`
      );
      this.$emit("toggleType", num);
    },
  },
};
